package com.example.administrator.android2019.Note;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.android2019.R;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener{

          ImageView back,save,delete;
          EditText ed_content;
         NoteSqlLiteHelper helper;
         TextView tv_name,tv_time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        init();
       helper=new NoteSqlLiteHelper(this);
       tv_name.setText("添加记录");
       Intent intent=this.getIntent();
       int id=intent.getIntExtra("id",-1);
       if (id>0){
           tv_name.setText("修改记录");
           tv_time.setText(intent.getStringExtra("time"));
           ed_content.setText(intent.getStringExtra("content"));
           /*布局文件中设置了隐藏，若是修改，设置可见*/
       }
    }

    private void init() {
        back=findViewById(R.id.note_record_image_back);
        save=findViewById(R.id.note_record_image_save);
        delete=findViewById(R.id.note_record_image_delete);
        ed_content=findViewById(R.id.note_record_ed_content);
        tv_name=findViewById(R.id.note_record_tv_name);
        tv_time=findViewById(R.id.note_record_tv_time);
        back.setOnClickListener(this);
        save.setOnClickListener(this);
        delete.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
       switch (v.getId()) {
           case R.id.note_record_image_back:
               finish();
               break;
           case R.id.note_record_image_save:

               String content = ed_content.getText().toString();
               Intent intent = this.getIntent();
               int id = intent.getIntExtra("id", -1);
               if (id > 0) {
                   if (!TextUtils .isEmpty(content)) {
                       if (helper.updateDate(id, content, helper.getTime())) {
                           setResult(2);
                           finish();
                           Toast.makeText(this, "修改成功！", Toast.LENGTH_SHORT).show();
                       } else {
                           Toast.makeText(this, "修改失败！", Toast.LENGTH_SHORT).show();
                       }
                   } else {
                       Toast.makeText(this, "内容不能为空！", Toast.LENGTH_SHORT).show();
                   }
               } else {
                   if (!TextUtils.isEmpty(content)) {
                       if (content != null) {
                           if (helper.insertDate(content, helper.getTime())) {
                               setResult(2);
                               finish();
                               Toast.makeText(this, "保存成功！", Toast.LENGTH_SHORT).show();
                           } else {
                               Toast.makeText(this, "保存失败！", Toast.LENGTH_SHORT).show();
                           }
                       } else {
                           Toast.makeText(this, "内容不能为空！", Toast.LENGTH_SHORT).show();
                       }
                   }
               }

               break;
           case R.id.note_record_image_delete:
               ed_content.setText("");
               break;
       }
    }
}
